<?php
namespace app\controller\api;

use app\model\api\PotterQuestion;
use laytp\controller\Api;
use think\exception\ValidateException;
/**
 * Class Potter
 * @package app\index\controller
 */
class Potter extends Api
{
    protected $model;

    protected function initialize()
    {
        $this->model = new PotterQuestion();
    }



    /**
     * 字母导航列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function tagList(){
        $data = input('post.');
        $type = !empty($data['type'])?$data['type']:'';

        try{
            //验证器
            validate(\app\validate\api\Potter::class)
                ->scene('list')
                ->check([
                    'type' => $type,
                ]);

            $list = $this->model->getTagList($type);
            return $this->success('success',$list);

        }catch (ValidateException $exception){
            return $this->error($exception->getMessage(),-1);
        }
    }


    /**
     * 问题列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list(){
        $data = input('post.');
        $keyword = !empty($data['keyword'])?$data['keyword']:'';
        $type = !empty($data['type'])?$data['type']:'';
        $guide = !empty($data['guide'])?$data['guide']:'';

        try{
            //验证器
            validate(\app\validate\api\Potter::class)
                ->scene('list')
                ->check([
                    'type' => $type,
                ]);

            $list = $this->model->list($type,$keyword,$guide);
            return $this->success('success',$list['data'],array(
                'total' => $list['total'],
                'current_page' => $list['current_page'],
                'last_page' => $list['last_page'],
                'per_page' => $list['per_page'],
            ));

        }catch (ValidateException $exception){
            return $this->error($exception->getMessage(),-1);
        }
    }

}
